"""
发送post请求
"""
import requests


class FanYi:
    def __init__(self, word):
        self.url = "http://fanyi.youdao.com/translate?smartresult=dict&smartresult=rule"
        self.word = word
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36"
        }
        self.data = {"i": self.word, "from": "AUTO", "to": "AUTO",
                     "smartresult": "dict",
                     "client": "fanyideskweb",
                     "salt": "16045770434639",
                     "sign": "6b82640fc6529bffe57198c3d509e709",
                     "lts": "1604577043463",
                     "bv": "0a19053d993d60580321f3e9b5652683",
                     "doctype": "json",
                     "version": "2.1",
                     "keyfrom": "fanyi.web",
                     "action": "FY_BY_REALTlME"
                     }

    def get_data(self):
        response = requests.post(self.url, headers=self.headers, data=self.data)
        return response.content.decode()


# 规范一般会加这句， “__name__”是Python的内置变量，用于指代当前模块
# __main__ 有点像C、Java的程序入口main()，Python中只要顶格就是入口开始执行
# A模块中若有该句；当B模块中导入A模块，执行时，A模块中的这部分语句就不会被执行
if __name__ == '__main__':
    fan = FanYi("好好学习 天天向上")
    fan2 = FanYi("work hard")
    print(fan.get_data())
    print('*' * 20)
    print(fan2.get_data())
